-- uu.lua
local x2hdr=Proto.new("x2hdr", "interface x2 header")

local channeltype_list = 
{
    [0x00] = "BCCH-BCH-Message",
    [0x01] = "BCCH-DL-SCH-Message",
    [0x02] = "MCCH-Message",
    [0x03] = "PCCH-Message",
    [0x04] = "DL-CCCH-Message",
    [0x05] = "DL-DCCH-Message",
    [0x06] = "UL-CCCH-Message",
    [0x07] = "DL-DCCH-Message",
}

local x2hdr_len = ProtoField.uint8("x2hdr.len", "len", base.DEC)
local x2hdr_srcenbid = ProtoField.uint32("x2hdr.src_enodeb_id", "src_enodeb_id", base.DEC)
local x2hdr_dstenbid = ProtoField.uint32("x2hdr.dst_enodeb_id", "dst_enodeb_id", base.DEC)
local x2hdr_sectorid = ProtoField.uint16("x2hdr.sectorid", "sectorid", base.DEC)
local x2hdr_reverse = ProtoField.string("x2hdr.reverse", "reverse")
local x2hdr_data = ProtoField.string("x2hdr.x2hdr_data", "x2hdr_data")

x2hdr.fields = {x2hdr_len, x2hdr_srcenbid, x2hdr_sectorid,
    x2hdr_dstenbid, x2hdr_reverse, x2hdr_data}

function x2hdr.dissector(buf, pinfo, tree)
    local total_len = buf:len()
    local offset = 0
    -- 取得专用头长度
    local len = buf(offset, 1):uint()
    -- 建立x2hdrtree
    x2hdrtree = tree:add(x2hdr, buf(offset, len + 1), "x2hdr tree")

    -- hdrlen
    x2hdrtree:add(x2hdr_len, buf(offset, 1))

    offset = offset + 1
    local left = len 

    -- src enodeb id
    x2hdrtree:add(x2hdr_srcenbid, buf(offset, 4))
    offset = offset + 4
    left = left - 4

    -- dst enodeb id
    x2hdrtree:add(x2hdr_dstenbid, buf(offset, 4))
    offset = offset + 4
    left = left - 4

    -- sector id
    x2hdrtree:add(x2hdr_sectorid, buf(offset, 2))
    offset = offset + 2
    left = left - 2

    -- reverse
    x2hdrtree:add(x2hdr_reverse, buf(offset, left), left.." bytes")
    offset = offset + left

    local dlen = total_len - offset
    if (dlen > 0) then
        local diss = Dissector.get("x2ap")
        Dissector.call(diss, buf(offset, dlen):tvb(), pinfo, x2hdrtree)
    end
end
